// pages/user/user.js
import { User } from '../../model/user.js';
var user = new User();
var appInst =  getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: false,
    userInfo: {},
    active: 2,
    icon: {
      home: '../../utils/image/home.png',
      home_active: '../../utils/image/home-active.png',
      order: '../../utils/image/order.png',
      order_active: '../../utils/image/order-active.png',
      user: '../../utils/image/user.png',
      user_active: '../../utils/image/user-active.png'
    }
  },
  /**
   * 切换菜单
   */
  onChangeTabbar(e){
    if(e.detail == 0) {
      appInst.goHome()
    }else  if(e.detail == 1) {
      wx.switchTab({
        url: '../order/order',
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideTabBar();
    wx.showLoading({
      title: '加载中',
    });
    this.getUserInfo();
  },
  /**
   * 修改title
   */
  onShow:function(){
    if(appInst.globalData.appstorename!='美业团')
      appInst.settitle();
  },
  /**
   * 获取用户信息
   */
  getUserInfo() {
    user.getUserInfo((result) => {
      this.setData({
        userInfo: result,
        hidden: true,
      })
      wx.hideLoading();
    })
  },
  /**
   * 更新用户信息
   */
  getUserProfile() {
    const self = this
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      lang: 'zh_CN',
      success: (res) => {
        user.updateUser({
          'nickname': res.userInfo.nickName,
          'head_img': res.userInfo.avatarUrl,
          'province': res.userInfo.province,
          'city': res.userInfo.city,
          'gender': res.userInfo.gender
        },(result) => {
          self.getUserInfo()
        })
      }
    })
    
  },

  /**
  * 获取手机号
  */
  getPhone: function (e) {
    if (e.detail.errMsg == 'getPhoneNumber:ok') {
      user.phonePost({ encrypted_data: e.detail.encryptedData, iv: e.detail.iv }, (isVild, result) => {
        if (isVild) {
          this.getUserInfo()
        } else {
          wx.showToast({
            title: '获取手机号失败,请稍后重试!',
            icon: 'none',
            duration: 2000
          })
          return false;
        }
      })
    } else {
      wx.showToast({
        title: '请允许获取您的手机号',
        icon: 'none',
        duration: 2000
      })
    }
  },
  // 优惠券列表
  openWinCou() {
    wx.navigateTo({
      url: 'coupon',
    })
  },
  // 打开余额页面
  openMoney() {
    wx.navigateTo({
      url: 'money',
    })
  },
  openWinStoreCode() {
    wx.navigateTo({
      url: 'code',
    })
  },
  openWinCode() {
    wx.navigateTo({
      url: 'team-code',
    })
  },
  openWinUser() {
    if(this.data.userInfo.type == 2) {
      wx.navigateTo({
        url: '../store/owner',
      })
    } else if (this.data.userInfo.type == 3 || this.data.userInfo.type == 4)  {
      wx.navigateTo({
        url: '../store/staff',
      })
    }  else if (this.data.userInfo.type == 5 || this.data.userInfo.type == 6)  {
      wx.navigateTo({
        url: '../team/staff',
      })
    }
  },
  openSystem() {
    wx.navigateTo({
      url: 'cnt',
    })
  },
  /**
   * 拨打电话
   */
  callPhone(e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone
    })
  }
})